﻿using System.Collections.Generic;
using WebSelfHost.Logic.Sys;
using WebSelfHost.Entity.Sys;
using WebSelfHost.Utility.ResponseModels;
using WebSelfHost.Utility.Core; 
using WebSelfHost.Logic;
using WebSelfHost.Utility.Operator;
using Microsoft.AspNetCore.Mvc;
using WebSelfHost.Entity.WebRequestParms.Sys.Role;
using WebSelfHost.Entity.WebRequestParms; 
using WebSelfHost.Web.Filters;
#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http; 
#endif
namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class RoleController : BaseController
    {
        private SysRoleLogic roleLogic;

        public RoleController()
        {
            roleLogic = new SysRoleLogic();
        }

        /// <summary>
        /// 角色管理主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/role/index"), AuthorizeChecked]
        public ActionResult Index()
        {
            return Html("/Sys/Role/Index");
        }

        /// <summary>
        /// 角色管理主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/role/index"), AuthorizeChecked]
        public ActionResult Index(RoleIndexParms parms)
        {
            int totalCount = 0;
            var pageData = roleLogic.GetList(parms.pageIndex, parms.pageSize, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysRole>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount// pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改角色主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/role/form"), AuthorizeChecked]
        public ActionResult Form()
        {
            return Html("/Sys/Role/Form");
        }

        /// <summary>
        /// 新增/修改角色主界面数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("system/role/form"), AuthorizeChecked]
        public ActionResult Form(SysRole model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = roleLogic.Insert(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = roleLogic.Update(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 角色管理主界面权限详细信息按钮方法
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/role/detail"), AuthorizeChecked]
        public ActionResult Detail()
        {
            return Html("/Sys/Role/Detail");
        }

        /// <summary>
        /// 根据主键得到角色信息
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/role/getForm"), LoginChecked]
        public ActionResult GetForm(PrimaryKeyParms parms)
        {
            SysRole entity = roleLogic.Get(parms.primaryKey);
            entity.IsEnabled = entity.IsEnabled == "1" ? "true" : "false";
            entity.AllowEdit = entity.AllowEdit == "1" ? "true" : "false";
            return Content(entity.ToJson());
        }


        /// <summary>
        /// 角色管理主界面删除权限按钮方法
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/role/delete"), AuthorizeChecked]
        public ActionResult Delete(PrimaryKeyParms parms)
        {
            int row = roleLogic.Delete(parms.primaryKey.SplitToList().ToArray());
            return row > 0 ? Success() : Error();
        }

        /// <summary>
        /// 角色授权主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/role/getListTreeSelect"), LoginChecked]
        public ActionResult GetListTreeSelect()
        {
            List<SysRole> listRole = roleLogic.GetList();
            var listTree = new List<TreeSelect>();
            foreach (var item in listRole)
            {
                TreeSelect model = new TreeSelect();
                model.id = item.Id;
                model.text = item.Name;
                listTree.Add(model);
            }
            return Content(listTree.ToJson());
        }

        /// <summary>
        /// 角色管理主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/role/index")]
        public ActionResult AppIndex(RoleIndexParms parms)
        {
            int totalCount = 0;
            var pageData = roleLogic.GetList(parms.pageIndex, parms.pageSize, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysRole>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount// pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改角色数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/role/form")]
        public ActionResult AppForm(SysRole model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = roleLogic.AppInsert(model, model.CreateUser);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = roleLogic.AppUpdate(model, model.ModifyUser);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 通过主键查询角色
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/role/getForm")]
        public ActionResult AppGetForm(PrimaryKeyParms parms)
        {
            SysRole entity = roleLogic.Get(parms.primaryKey);
            entity.IsEnabled = entity.IsEnabled == "1" ? "true" : "false";
            entity.AllowEdit = entity.AllowEdit == "1" ? "true" : "false";
            return Content(entity.ToJson());
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/role/delete")]
        public ActionResult AppDelete(RoleDeleteParms parms)
        {
            int row = roleLogic.Delete(parms.primaryKey.SplitToList().ToArray());
            Logger.OperateInfo($"用户{parms.operateUser}删除了角色");
            return row > 0 ? Success() : Error();
        }

        /// <summary>
        /// 获取角色授权列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("app/system/role/getListTreeSelect")]
        public ActionResult AppGetListTreeSelect()
        {
            List<SysRole> listRole = roleLogic.GetList();
            var listTree = new List<TreeSelect>();
            foreach (var item in listRole)
            {
                TreeSelect model = new TreeSelect();
                model.id = item.Id;
                model.text = item.Name;
                listTree.Add(model);
            }
            return Content(listTree.ToJson());
        }
    }
}
